package com.oi.eksekutif.dao.impl;

import com.oi.eksekutif.dao.PengemudiDAO;
import com.oi.eksekutif.domain.Pengemudi;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Deny Prasetyo
 * Date: Apr 21, 2010
 * Time: 1:28:30 AM
 */
@Repository
@Transactional
public class PengemudiDAOImpl implements PengemudiDAO {

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;

    @Override
    public long count() {
         return (Long)this.entityManager.createQuery("SELECT count(o) FROM Pengemudi o").getSingleResult();
    }

    @Override
    public List<Pengemudi> findAll() {
        return this.entityManager.createQuery("SELECT o FROM Pengemudi o").getResultList();
    }

    @Override
    public List<Pengemudi> findEntries(int firstResult, int maxResults) {
        return this.entityManager.createQuery("SELECT o FROM Pengemudi o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Override
    public Pengemudi find(Long id) {
        return entityManager.find(Pengemudi.class,id);
    }

    @Override
    public Pengemudi findByNomorKPP(String nomorKpp) {
        return (Pengemudi) this.entityManager.createQuery("SELECT o FROM Pengemudi o WHERE o.nomorKpp =:nomorKpp").setParameter("nomorKpp", nomorKpp).getSingleResult();
    }

    @Override
    public void persist(Pengemudi o) {
        this.entityManager.persist(o);
    }

    @Override
    public void merge(Pengemudi o) {
        this.entityManager.merge(o);
    }

    @Override
    public void flush() {
        this.entityManager.flush();
    }

    @Override
    public void remove(Pengemudi o) {
        this.entityManager.remove(this.entityManager.merge(o));
    }
}